import json
from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
from django.views.decorators.csrf import csrf_exempt

from user.models import User


@csrf_exempt
def user_signin(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf8'))
        user_account = data.get("account")
        user_password = data.get("password")
        user_email = data.get("email")
        filter_account = User.objects.filter(account=user_account)
        filter_email = User.objects.filter(email=user_email)
        # 验证账户是否已经被注册
        if filter_account.exists():
            return JsonResponse({"error": "该账户已注册"}, status=405)
        # 验证邮箱是否已经被注册
        if filter_email.exists():
            return JsonResponse({"error": "该邮箱已注册"}, status=405)
        # 添加新用户，同时定义返回值
        new_user = User(
            account=user_account,
            password=user_password,
            email=user_email,
        )
        new_user.save()
        return_data = {"id": new_user.id}
        return JsonResponse(return_data, status=200)
    elif request.method == 'OPTION':
        return JsonResponse({"success": "OPTION operation"}, status=200)
    else:
        return JsonResponse({"error": "Method not allowed"}, status=405)


@csrf_exempt
def user_login(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf8'))
        user_account = data.get("account")
        user_password = data.get("password")
        filter_account = User.objects.filter(account=user_account)
        # 验证数据库中是否存在该用户
        if not filter_account.exists():
            return JsonResponse({"error": "该账户不存在"}, status=405)
        # 验证密码是否正确
        if user_password != filter_account[0].password:
            return JsonResponse({"error": "密码错误"}, status=405)
        # 定义返回值
        return_data = {
            "id": filter_account[0].id,
            "account": filter_account[0].account,
            "email": filter_account[0].email,
            "phone_number": filter_account[0].phone_number,
            "sex": filter_account[0].phone_number,
        }
        return JsonResponse(return_data, status=200)
    elif request.method == 'OPTION':
        return JsonResponse({"success": "OPTION operation"}, status=200)
    else:
        return JsonResponse({"error": "Method not allowed"}, status=405)


@csrf_exempt
def get_user_info(request):
    if request.method == 'POST':
        data = json.loads(request.body.decode('utf8'))
        user_id = data.get('user_id')
        filter_user = User.objects.filter(id=int(user_id))
        if filter_user.exists():
            return_data = {
                'user_id': filter_user[0].id,
                'account': filter_user[0].account,
                'user_email': filter_user[0].email,
            }
            return JsonResponse(return_data, status=200)
        else:
            return JsonResponse({"error": "User not exist"}, status=405)
    elif request.method == 'OPTION':
        return JsonResponse({"success": "OPTION operation"}, status=200)
    else:
        return JsonResponse({"error": "Method not allowed"}, status=405)